package com.sdry.service.impl.hpf;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.hpf.PositionMapper;
import com.sdry.mapper.hpf.ProductMapper;
import com.sdry.mapper.hpf.ReferenceMapper;
import com.sdry.mapper.hpf.TechnicsMapper;
import com.sdry.model.hpf.Product;
import com.sdry.model.hpf.Reference;
import com.sdry.model.hpf.Technics;
import com.sdry.model.llm.User;
import com.sdry.service.hpf.ReferenceService;

/**
 * 作业指导书service实现类
 * 
 * @author lian
 * @version 1.0
 * @time 2018年10月12日 上午10:58:41
 */

@Service
public class ReferenceServiceImp implements ReferenceService {

	@Resource
	private ReferenceMapper referenceMapper;
	@Resource
	private PositionMapper positionMapper;
	@Resource
	private ProductMapper productMapper;
	@Resource
	private TechnicsMapper technicsMapper;

	/**
	 * 查询作业指导书数据总行数
	 * @param reference 作业指导书
	 * @return
	 */
	@Override
	public int countReference(Reference reference) {
		return referenceMapper.countReference(reference);
	}

	/**
	 * 分页查询作业指导书信息
	 * @param map  作业指导书数据集合
	 * @return
	 */
	@Override
	public List<Reference> listPageReference(Map<String, Object> map) {
		List<Reference> list = referenceMapper.listPageReference(map);
		for(Reference re : list){
			/*if(null != re.getPositionId()){
				Position position = positionMapper.getPositionByPid(re.getPositionId());
				re.setPositionNumber(position.getPname());
			}
			if(null != re.getProductId()){
				Product product = productMapper.getProductByPid(re.getProductId());
				re.setProductNumber(product.getPname());
			}*/
			if(null != re.getPositionId()){
				Technics technics = technicsMapper.getTechnicsByTid(re.getPositionId());
				re.setPositionNumber(technics.getTname());
			}
		}
		return list;
	}

	/**
	 * 保存作业指导书数据
	 * @param reference 作业指导书
	 * @return
	 */
	@Override
	public int insertReference(Reference reference,HttpSession session) {
		int isSuccess = -2;
		if(!"".equals(reference.getPositionId()) && !"".equals(reference.getProductId()) && !"".equals(reference.getEngineeringEnum())){
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String time = sdf.format(date);
			reference.setRproductionTime(time);
			User user = (User)session.getAttribute("currentUser");
			if(null != user){
				reference.setRproducer(user.getUserName());
			}
			List<Reference> list = referenceMapper.getReferenceByPositionId(reference.getPositionId());
			if(list.size() <= 0){
				int i = referenceMapper.insertReference(reference);
				if(i > 0){
					isSuccess = 2;
				}
			}
		}
		return isSuccess;
	}

	/**
	 * 修改作业指导书数据
	 * @param reference 作业指导书
	 * @return
	 */
	@Override
	public int updateReference(Reference reference) {
		int isSuccess = -3;
		int i = referenceMapper.updateReference(reference);
		if(i > 0){
			isSuccess = 3;
		}
		return isSuccess;
	}

	/**
	 * 删除作业指导书数据
	 * @param rid 作业指导书ID
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public int deleteReference(String str) {
		int isSuccess = 0;
		String[] split = str.split(",");
		try {
			for (String rid : split) {
				isSuccess = referenceMapper.deleteReference(Long.parseLong(rid));
			}
		}catch (
		Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;

	}

	/**
	 * 查询所有的作业指导书
	 * @return
	 */
	@Override
	public List<Product> listAllReference() {
		// TODO Auto-generated method stub
		return referenceMapper.listAllReference();
	}

	/**      
	 * 根据ID查询工艺详情 
	 * @param positionId
	 * @return    
	 */
	@Override
	public Reference getReferenceById(Long positionId) {
		// TODO Auto-generated method stub
		return referenceMapper.getReferenceById(positionId);
	}

	/**   
	 * 根据名称和版本号查询工艺详情    
	 * @param reference
	 * @return    
	 */
	@Override
	public Reference getReferenceByNameAndNum(Reference reference) {
		return referenceMapper.getReferenceByNameAndNum(reference);
	}

}
